#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import os
import urllib

import webapp2
import jinja2
import webapp2

from google.appengine.api import users
from google.appengine.ext import ndb

JINJA_ENVIRONMENT = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
    extensions=['jinja2.ext.autoescape'],
    autoescape=True)

class ConfigSettingHandler(webapp2.RequestHandler):
    def get(self):
        import json
        result = json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])
        self.response.content_type = "application/json"
        #self.response.write(result)
        # self.response.charset = "utf-8"
        template_values = {
            'user': 'z',
            'greetings': 'hwlo',
            'url': 'http://www.uajp.com',
            'url_linktext': 'asd',
        }
        import pdb
        pdb.set_trace()
        template = JINJA_ENVIRONMENT.get_template('json/comment.json')
        self.response.write(template.render(template_values))
        #self.response.write('Hello ConfigSettingHandler!')


class webService(webapp2.RequestHandler):
    """ STATUS: Demo
        DESCRIPTION:
            Demostration of a CORS web service
    """

    def get(self):
        self.response.headers.add_header('Access-Control-Allow-Origin', '*')
        self.response.headers['Content-Type'] = 'application/json'
        # do something

    def post(self):
        self.response.headers.add_header('Access-Control-Allow-Origin', '*')
        self.response.headers['Content-Type'] = 'application/json'
        # do something

    def options(self):
        self.response.headers['Access-Control-Allow-Origin'] = '*'
        self.response.headers['Access-Control-Allow-Headers'] = 'Origin, X-Requested-With, Content-Type, Accept'
        self.response.headers['Access-Control-Allow-Methods'] = 'POST, GET, PUT, DELETE'

app = webapp2.WSGIApplication([
    ('/ws/config', ConfigSettingHandler)
], debug=True)
